﻿@using System.Globalization
@using LERP.Domain.Enums
@using Sharp.Framework
@using Sharp.Framework.Extensions
@model LERP.Web.Controllers.ViewModels.OrderViewModels.ReceivedOrderViveModel

@{
    ViewBag.Title = "订单收货";
    Layout = "~/Views/Shared/_Layout.cshtml"; 
}
<div class="empty" style="text-align: left;background-color: #FFFCEB;font-size: 14px;font-family: 宋体,Arial,Lucida,Verdana,Helvetica,sans-serif;">
    
    <strong>订单编号: @this.ViewContext.RouteData.Values["id"] </strong>    <strong>状态: @(((OrderStatus)Model.Status).GetDescription())   </strong> 
</div>
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset> 
        <legend>基本信息 </legend>
        <ul class="list_form">
            <li>
                @Html.LabelFor(model => model.Customer) 
                @Model.Customer
                @Html.HiddenFor(model => model.Customer)
            </li>
          <li>
                @Html.LabelFor(model => model.ContractName)   
                @Model.ContractName
                @Html.HiddenFor(model => model.ContractName) 
            </li>
            <li>
                @Html.LabelFor(model => model.OrderDate)  
                @Model.OrderDate
                @Html.HiddenFor(model => model.OrderDate)  
            </li>
        
            <li>
                @Html.LabelFor(model => model.ExpectDelivery)  
                @Html.TextBoxFor(model => model.ExpectDelivery, new { @class = "text-long dataselect" })
                @Html.ValidationMessageFor(model => model.ExpectDelivery)
                
            </li> 
            <li>
                @Html.LabelFor(model => model.TangibleDelivery)  
                @Html.TextBoxFor(model => model.TangibleDelivery, new { @class = "text-long dataselect" })
                @Html.ValidationMessageFor(model => model.TangibleDelivery) 
            </li> 
            <li>
                @Html.LabelFor(model => model.CustomerOrderNo)    
                @Model.CustomerOrderNo
                @Html.HiddenFor(model => model.CustomerOrderNo)  
            </li> 
            <li>
                @Html.LabelFor(model => model.SafeNo)   
                  @Model.SafeNo
                @Html.HiddenFor(model => model.SafeNo)  
                    
            </li>
            <li>
                @Html.LabelFor(model => model.DeliveryNo)   
                  @Model.DeliveryNo
                @Html.HiddenFor(model => model.DeliveryNo)  
            </li>
            <li>
                @Html.LabelFor(model => model.LogisticsProvider) 
                  @Model.LogisticsProvider
                @Html.HiddenFor(model => model.LogisticsProvider)  

            </li>
            <li>
                @Html.LabelFor(model => model.CarrierCode)   
                  @Model.CarrierCode
                @Html.HiddenFor(model => model.CarrierCode)  

            </li> 
            <li>
                @Html.LabelFor(model => model.Remark)   
                @Html.TextAreaFor(model => model.Remark, new { rows = "1", cols = "1" })
                @Html.ValidationMessageFor(model => model.Remark)
                    
                @Html.HiddenFor(model => model.IsGoods)  
            </li>
            @if(!Model.IsGoods)
            {
                <li>
                    @Html.LabelFor(model => model.Amount)   
                    @Model.Amount 
                </li>
            }
           
            
        </ul> 
    </fieldset>
      
          <fieldset id="fieldset_goods" style="display: @((Model.IsGoods) ? "" : "none");"> 
              <legend>货品明细</legend> 
              <table class="table_list" id="goods_table" style="width: 99%; display: none;">
                  <tr>
                      <th style="width: 80px;">国际条码</th>
                      <th>品名规格</th>
                      <th style="width: 80px;">单价</th>
                      <th style="width: 30px;">单位</th>
                      <th style="width: 60px;">订单数量</th> 
                          <th style="width: 60px;">发货数量</th> 
                          <th style="width: 60px;">实收数量</th> 
                  </tr>             
              </table> 
              <div style="float: right; margin-top: 25px; width: 300px; text-align: right;">
                  <div style="margin-bottom: 5px;">订单金额: <label id="order_sum">0</label> 元 
                      @Html.HiddenFor(model => model.OrderItems)
                  </div> 
                  <div style="margin-bottom: 5px;">发货金额: <label id="order_CarrierAmount">0</label> 元</div>
                   <div style="margin-bottom: 5px;">门店收货金额: <label id="order_PaymentAmount">0</label> 元</div>
                   <div style="margin-bottom: 5px;">租借出仓金额: <label id="order_WarehouseAmount">0</label> 元</div> 
              </div>
          </fieldset> 
    <input type="submit"  class="button-submit" value="确认收货" onclick="return window.confirm('您确认要为当前订单的收货信息么?');" />  
    <br/>
    
<br/> 
}
@section scripts
{ 
    <script type="text/javascript">
        @{
            var templateString = "<tr id=\\\"goods_{0}\\\"><td>{1}</td><td title=\\\"{2}\\\">{3}</td><td>{4}</td><td>{5}</td><td>{6}</td><td>{7}</td><td><input class=\\\"text-small\\\"   goodsid=\\\"{0}\\\"  name=\\\"rnum\\\" type=\\\"text\\\" value=\\\"{8}\\\"></td></tr>";
                     } 
        var goodsitemtemplate = "@(new MvcHtmlString(templateString))";
        var goodsList = new Array();

        $(function () {
            //加载返回的订单项
            var $orderItems = $("#@Html.IdFor(m => m.OrderItems)");
            if ($orderItems.val() != '') {
                goodsList = JSON.parse($orderItems.val());
                for (var i = 0; i < goodsList.length; i++) {
                    var item = goodsList[i];
                    $("#goods_table").append(String.format(goodsitemtemplate, item.id, item.gencod, item.name, item.shortname, item.price, item.sellunit, item.num, item.cnum, item.rnum));
                }
                $("#goods_table").show();
                statisticsOrders();
            }
            
            //修改货品数量
            $("#goods_table input.text-small").live("change", function () {
                if (!new RegExp("^\\d+$").test(this.value)) {
                    this.value = this.value.replace(/\D/g, '');
                    alert("只能输入正整数");
                    return false;
                }
                var goodsid = $(this).attr("goodsid");
                for (var i = 0; i < goodsList.length; i++) {
                    var item = goodsList[i];
                    if (item.id == goodsid) {
                        item[this.name] = $(this).val();
                        break;
                    }
                }
                statisticsOrders();
                return false;
            });
            

        });

         
        var statisticsOrders = function () {
            var sum = 0;
            var carrierAmount = 0;
            var paymentAmount = 0;
            for (var i = 0; i < goodsList.length; i++) {
                var item = goodsList[i];
                sum += new Number(item.price) * new Number(item.num);
                carrierAmount += new Number(item.price) * new Number(item.cnum);
                paymentAmount += new Number(item.price) * new Number(item.rnum);
            }
            $("#order_sum").html(sum);
            $("#order_CarrierAmount").html(carrierAmount);
            $("#order_PaymentAmount").html(paymentAmount);
            $("#order_WarehouseAmount").html(carrierAmount - paymentAmount);
            $("#@Html.IdFor(m => m.Amount)").val(sum);
            $("#@Html.IdFor(m => m.OrderItems)").val(JSON.stringify(goodsList));
        };

    </script>

} 